use libc::{c_void, size_t, c_int, c_char, ptrdiff_t, c_double};

#[allow(non_camel_case_types)]
pub type lua_Integer = i64;

#[allow(non_camel_case_types)]
pub type lua_Number = c_double;

#[allow(non_camel_case_types)]
pub type lua_State = c_void;

#[allow(non_camel_case_types)]
pub type lua_Alloc = Option<unsafe extern "C" fn(ud: *mut c_void, ptr: *mut c_void, osize: size_t, nsize: size_t) -> *mut c_void>;

#[allow(non_camel_case_types)]
pub type lua_KContext = ptrdiff_t;

#[allow(non_camel_case_types)]
pub type lua_KFunction = Option<unsafe extern "C" fn(L: *mut lua_State, status: c_int, ctx: lua_KContext) -> c_int>;

#[allow(non_camel_case_types)]
pub type lua_CFunction = Option<unsafe extern "C" fn(L: *mut lua_State) -> c_int>;

#[repr(C)]
#[allow(non_camel_case_types)]
pub struct luaL_Reg {
  pub name: *const c_char,
  pub func: lua_CFunction,
}

// Links to LUA_API
extern {
    pub fn luaL_newstate() -> *mut lua_State;
    pub fn luaL_openlibs(L: *mut lua_State);

    pub fn lua_rawset(L: *mut lua_State, idx: c_int);
    
    pub fn lua_gettop(L: *mut lua_State) -> c_int;
    pub fn lua_type(L: *mut lua_State, idx: c_int) -> c_int;
    pub fn lua_pushvalue(L: *mut lua_State, idx: c_int);

    pub fn lua_pushstring(L: *mut lua_State, s: *const c_char) -> *const c_char;
    pub fn lua_tolstring(L: *mut lua_State, idx: c_int, len: *mut size_t) -> *const c_char;
    
    pub fn lua_settable(L: *mut lua_State, idx: c_int);
    pub fn lua_gettable(L: *mut lua_State, idx: c_int) -> c_int;

    pub fn lua_toboolean(L: *mut lua_State, idx: c_int) -> c_int;

    pub fn lua_setglobal(L: *mut lua_State, name: *const c_char) -> c_int;
    pub fn lua_setfield(L: *mut lua_State, idx: c_int, k: *const c_char);

    pub fn lua_pushinteger(L: *mut lua_State, n: lua_Integer) -> c_int;
    pub fn lua_tointegerx(L: *mut lua_State, idx: c_int, pisnum: *mut c_int) -> lua_Integer;

    pub fn lua_pushnumber(L: *mut lua_State, n: lua_Number);
    pub fn lua_tonumberx(L: *mut lua_State, idx: c_int, pisnum: *mut c_int) -> lua_Number;

    pub fn lua_pushlightuserdata(L: *mut lua_State, p: *mut c_void);
    pub fn lua_touserdata(L: *mut lua_State, idx: c_int) -> *mut c_void;

    pub fn lua_createtable(L: *mut lua_State, narr: c_int, nrec: c_int);
    pub fn lua_pushcclosure(L: *mut lua_State, func: lua_CFunction, n: c_int);

    pub fn luaL_newmetatable(L: *mut lua_State, tname: *const c_char) -> c_int;
    pub fn luaL_setmetatable(L: *mut lua_State, tname: *const c_char);

    pub fn lua_settop(L: *mut lua_State, idx: c_int);

    pub fn luaL_loadstring(L: *mut lua_State, s: *const c_char) -> c_int;
    pub fn lua_pcallk(L: *mut lua_State, nargs: c_int, nresults: c_int, errfunc: c_int, ctx: lua_KContext, k: lua_KFunction) -> c_int;

    pub fn lua_newstate(f: lua_Alloc, ud: *mut c_void) -> *mut lua_State;
}

extern {
    pub fn luaopen_base(L: *mut lua_State) -> c_int;
    pub fn luaopen_table(L: *mut lua_State) -> c_int;
    pub fn luaopen_package(L: *mut lua_State) -> c_int;
    pub fn luaopen_io(L: *mut lua_State) -> c_int;
    pub fn luaopen_string(L: *mut lua_State) -> c_int;
}

extern {
    pub fn lua_isinteger(L: *mut lua_State, idx: c_int) -> c_int;
}

pub const LUA_MULTRET: c_int = -1;
#[inline(always)]
#[allow(non_snake_case)]
pub unsafe fn luaL_dostring(L: *mut lua_State, s: *const c_char) -> c_int {
    let status = luaL_loadstring(L, s);
    if status == 0 {
        lua_pcall(L, 0, LUA_MULTRET, 0)
    } else {
        status
    }
}
#[inline(always)]
#[allow(non_snake_case)]
pub unsafe fn lua_pcall(L: *mut lua_State, n: c_int, r: c_int, f: c_int) -> c_int {
    lua_pcallk(L, n, r, f, 0, None)
}